SET(ODBC_SOURCE_WIX_DIR ${CMAKE_SOURCE_DIR}/wininstall)

# Get revision number
IF(WITH_REVNO)
  EXECUTE_PROCESS(COMMAND git log HEAD^^..HEAD
                  COMMAND FINDSTR commit
  OUTPUT_VARIABLE revno)
  STRING(REPLACE "commit " "" revno ${revno})
  STRING(REPLACE "\n" "" revno ${revno})
ENDIF()

IF(NOT WIX_DIR)
  SET(WIX_DIR "$ENV{WIX}/bin/")
  MESSAGE(STATUS "WiX directory: ${WIX_DIR}")
ENDIF()
SET(PRODUCT_NAME "openLooKeng ODBC Driver")
SET(PRODUCT_MANUFACTURER "openLooKeng")
SET(PRODUCT_VERSION "${MARIADB_ODBC_VERSION_MAJOR}.${MARIADB_ODBC_VERSION_MINOR}.${MARIADB_ODBC_VERSION_PATCH}")

IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
  SET(PRODUCT_NAME "${PRODUCT_NAME} 64-bit")
  SET(PLATFORM "win64")
  SET(IS_WIN64 "yes")
  SET(IS64 "64")
  SET(WIXPLATFORM "x64")
  SET(FOLDER "ProgramFiles64Folder")
  SET(GUID_REGISTRY "7D5E1BED-272C-4D07-901A-36D9E439EF8A")
  SET(GUID_SETUP "D7AD9D03-C82B-4F13-9DD2-7436E705310C")
  SET(GUID_DRIVER "F62D61A2-834F-41CC-94C0-7D3F15404EC6")
  SET(GUID_DEBUG "26C1958F-B255-473A-A8A7-A24137947942")
  SET(GUID_INSTALLER_TOOLS "3EF14C90-23ED-480D-994F-5D284290CBC9")
ELSE()
  SET(PLATFORM "win32")
  SET(IS_WIN64 "no")
  SET(IS64 "")
  SET(WIXPLATFORM "x86")
  SET(FOLDER "ProgramFilesFolder")
  SET(GUID_REGISTRY "EE85A53C-3DA8-458E-A9D9-AA0F2DE2954F")
  SET(GUID_SETUP "356DB4E9-AD08-47C7-BD9D-93F8D3D61708")
  SET(GUID_DRIVER "2652D05F-AF07-49F6-B08D-BB5767819FB5")
  SET(GUID_DEBUG "46D2292A-994D-443E-AD31-8C05CBD1C09D")
  SET(GUID_INSTALLER_TOOLS "8E487BC4-EB89-4866-9FFD-0A3375CBD542")
ENDIF()

ADD_EXECUTABLE(change_dsns_driver change_dsns_driver.c ${CMAKE_SOURCE_DIR}/ma_dsn.c ${CMAKE_SOURCE_DIR}/ma_platform_win32.c ${CMAKE_SOURCE_DIR}/ma_common.c)
TARGET_LINK_LIBRARIES(change_dsns_driver ${ODBC_LIBS} ${ODBC_INSTLIBS} legacy_stdio_definitions Shlwapi)

CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/wininstall/mariadb_odbc.xml.in
               ${CMAKE_BINARY_DIR}/wininstall/mariadb_odbc.xml)
IF(${revno})
  SET(MSI_PACKAGE "hetu-odbc-r${revno}-${PLATFORM}-010.msi")
ELSE()
  SET(MSI_PACKAGE "hetu-odbc-${PLATFORM}-010.msi")
ENDIF()

SET(ENV{MARIADB_ODBC_MSI_PACKAGE} "${MSI_PACKAGE}")

IF(WITH_SIGNCODE)
  IF(EXISTS "/tools/sign.bat")
    ADD_CUSTOM_TARGET(SIGNMSI
      DEPENDS ${MSI_PACKAGE}
      COMMAND /tools/sign.bat ${MSI_PACKAGE})
  ELSE()
    ADD_CUSTOM_TARGET(SIGNMSI
      DEPENDS ${MSI_PACKAGE}
      COMMAND signtool sign ${SIGN_OPTIONS} ${MSI_PACKAGE})
  ENDIF()
  ADD_DEPENDENCIES(SIGNMSI ${MSI_PACKAGE})
  SET_TARGET_PROPERTIES(SIGNMSI PROPERTIES EXCLUDE_FROM_ALL OFF)
ENDIF()

MESSAGE(STATUS "MSI package name ${MSI_PACKAGE}")

ADD_CUSTOM_TARGET(
        ${MSI_PACKAGE}
        COMMAND ${WIX_DIR}light.exe -ext WixUIExtension -ext WixUtilExtension mariadb_odbc.wixobj -o ${MSI_PACKAGE})

SET_TARGET_PROPERTIES(${MSI_PACKAGE} PROPERTIES EXCLUDE_FROM_ALL OFF)

ADD_CUSTOM_TARGET(
        ODBC_WIX
        DEPENDS mariadb_odbc.xm binaries_dir.xm
        COMMAND ${WIX_DIR}candle.exe -ext WixUIExtension -ext WixUtilExtension mariadb_odbc.xml -o mariadb_odbc.wixobj)

ADD_DEPENDENCIES(${MSI_PACKAGE} ODBC_WIX)
ADD_DEPENDENCIES(ODBC_WIX maodbc maodbcs change_dsns_driver)
IF(NOT USE_SYSTEM_INSTALLED_LIB)
  ADD_DEPENDENCIES(ODBC_WIX maodbc maodbcs dialog)
ENDIF()

